import com.MaterialHeap.User
import com.MaterialHeap.UserRole
import com.MaterialHeap.Role
import grails.util.Environment
import grails.util.GrailsUtil
import com.MaterialHeap.location.Province
import com.MaterialHeap.location.City

class BootStrap {

    def init = { servletContext ->
        // create Province
        def hubei = new Province([name: "湖北"]).save(failInError: true);
        def hunan = new Province([name: "湖南"]).save(failInError: true);
        def guangdong = new Province([name: "广东"]).save(failInError: true);
        def guangxi = new Province([name: "广西"]).save(failInError: true);
        def beijing = new Province([name: "北京"]).save(failInError: true);
        def shanghai = new Province([name: "上海"]).save(failInError: true);

        // create City
        def wuhan = new City([name: "武汉", province: hubei]).save(failInError: true);
        def yichang = new City([name: "宜昌", province: hubei]).save(failInError: true);
        def xiaogan = new City([name: "孝感", province: hubei]).save(failInError: true);
        def jingmen = new City([name: "荆门", province: hubei]).save(failInError: true);

        // create user and role
        def adminRole = Role.findByAuthority(Role.ADMIN);
        if (!adminRole) {
            adminRole = new Role(authority: Role.ADMIN).save(failInError: true);
            assert adminRole.id;
        }
        createTestUser("gyh", "gyh", "gyh@test.org", "83856318", hubei, wuhan, "江汉区江汉路118号港澳中心新世界时尚广场(近江汉二路)", adminRole)

        def managerRole = Role.findByAuthority(Role.MANAGER);
        if (!managerRole) {
            managerRole = new Role(authority: Role.MANAGER).save(failInError: true);
            assert managerRole.id;
        }

        def merchantRole = Role.findByAuthority(Role.CUSTOMER);
        if (!merchantRole) {
            merchantRole = new Role(authority: Role.CUSTOMER).save(failInError: true);
            assert merchantRole.id;
        }

        def userRole = Role.findByAuthority(Role.CUSTOMER);
        if (!userRole) {
            userRole = new Role(authority: Role.CUSTOMER).save(failInError: true);
            assert userRole.id;
        }

        if (Environment.current != Environment.PRODUCTION) {
            createTestUser("gyh", "gyh", "gyh@test.org", "83856318", hubei, wuhan, "江汉区江汉路118号港澳中心新世界时尚广场(近江汉二路)", adminRole)
            createTestUser("kenobi", "kenobi", "kenobi@test.org", "83856318", hubei, wuhan, "江汉区江汉路118号港澳中心新世界时尚广场(近江汉二路)", managerRole)
            createTestUser("skywalker", "skywalker", "skywalker@test.org", "83856318", hubei, wuhan, "江汉区江汉路118号港澳中心新世界时尚广场(近江汉二路)", merchantRole)
            createTestUser("cloneman", "cloneman", "cloneman@test.org", "83856318", hubei, wuhan, "江汉区江汉路118号港澳中心新世界时尚广场(近江汉二路)", userRole)
        }
    }

    private createTestUser(String username, String password, String email, String phoneNumber,
                           Province province, City city, String address, Role role) {
        User user = User.findByUsername(username);

        if (!user) {
            user = new User(username: username, password: password,email: email,
                    phoneNumber: phoneNumber, province: province, city: city, address: address,
                    enabled: true, passwordExpired: false, accountLocked: false, accountExpired: false)
            user.save(failInError: true);
            assert user.id;
        }

        def adminUserRole = UserRole.findByUserAndRole(user, role);

        if (!adminUserRole) {
            UserRole.create(user, role, true);
        }

        return user
    }

    def destroy = {
    }
}
